﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Sirian_ScriptEditor.Dialog.WaveDialog;

namespace Sirian_ScriptEditor.Dialog
{
    public partial class CreateWave : Form
    {
        public int time;
        public string formation;
        public string aircrafts = "";
        public string path = "";

        public CreateWave()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (comboFormation.SelectedIndex > -1 && lstAircraft.Items.Count > 0)
            {
                formation = comboFormation.SelectedItem.ToString();
                time = (int)numericGameTime.Value;
                int i = 0;
                foreach (string item in lstAircraft.Items)
                {
                    aircrafts += item + (++i < lstAircraft.Items.Count ? "," : "");
                }
                i = 0;
                foreach (string item in lstPath.Items)
                {
                    path += item + (++i < lstPath.Items.Count ? "|" : "");
                }
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void lstAircraft_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void CreateWave_Load(object sender, EventArgs e)
        {

        }

        private void btnAicraftUp_Click(object sender, EventArgs e)
        {
            int index = lstAircraft.SelectedIndex;
            if (index > 0)
            {
                string item = lstAircraft.SelectedItem.ToString();
                lstAircraft.Items.RemoveAt(index);
                index--;
                lstAircraft.Items.Insert(index, item);
            }
        }

        private void btnAicraftDown_Click(object sender, EventArgs e)
        {
            int index = lstAircraft.SelectedIndex;
            if (index > -1 && index + 1 < lstAircraft.Items.Count)
            {
                string item = lstAircraft.SelectedItem.ToString();
                lstAircraft.Items.RemoveAt(index);
                index++;
                lstAircraft.Items.Insert(index, item);
            }
        }

        private void btnAicraftAdd_Click(object sender, EventArgs e)
        {
            CreateAicraft dialogAircraft = new CreateAicraft();
            dialogAircraft.Owner = this;
            if (dialogAircraft.ShowDialog() == DialogResult.OK)
            {
                string item = dialogAircraft.aicraft;
                item += dialogAircraft.item == "" ? "": "|" + dialogAircraft.item;
                lstAircraft.Items.Add(item);
            }
        }

        private void btnAicraftRemove_Click(object sender, EventArgs e)
        {
            if (lstAircraft.SelectedIndex > -1)
            {
                lstAircraft.Items.Remove(lstAircraft.SelectedItem);
            }
        }

        private void btnPathDown_Click(object sender, EventArgs e)
        {
            int index = lstPath.SelectedIndex;
            if (index > 0 && index + 1 < lstPath.Items.Count)
            {
                string item = lstPath.SelectedItem.ToString();
                lstPath.Items.RemoveAt(index);
                index++;
                lstPath.Items.Insert(index, item);
            }
        }

        private void btnPathUp_Click(object sender, EventArgs e)
        {
            int index = lstPath.SelectedIndex;
            if (index > 1)
            {
                string item = lstPath.SelectedItem.ToString();
                lstPath.Items.RemoveAt(index);
                index--;
                lstPath.Items.Insert(index, item);
            }
        }

        private void btnPathRemove_Click(object sender, EventArgs e)
        {
            if (lstPath.SelectedIndex > 0)
            {
                lstPath.Items.Remove(lstPath.SelectedItem);
            }
        }

        private void btnPathAdd_Click(object sender, EventArgs e)
        {
            CreatePoint dialogPoint = new CreatePoint();
            dialogPoint.Owner = this;
            if (dialogPoint.ShowDialog() == DialogResult.OK)
            {
                lstPath.Items.Add(dialogPoint.point.X + "," + dialogPoint.point.Y);
            }
        }

        private void lstPath_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
